package cc.rengu.igas.share.core.service.channel.momp;

import cc.rengu.igas.momp.facade.base.Header;
import cc.rengu.igas.momp.facade.bean.PaymentOrderCouponInfoBean;
import cc.rengu.igas.momp.facade.bean.PaymentOrderDetailBean;
import cc.rengu.igas.momp.facade.request.ComboPaymentRequest;
import cc.rengu.igas.momp.facade.response.ComboPaymentResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.share.common.constant.ShareServiceConstant;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.igas.share.common.constant.UserSessionConstant;
import cc.rengu.igas.share.common.enums.FeeTypeEnum;
import cc.rengu.igas.share.core.model.CouponInfo;
import cc.rengu.igas.share.core.realize.UserService;
import cc.rengu.igas.share.core.realize.impl.UserServiceImpl;
import cc.rengu.oltp.service.base.ChannelBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.*;
import cc.rengu.oltp.service.common.enums.*;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.model.TxnInfo;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;

import java.util.*;

/**
 * 混合支付(对客及对商户营销)
 * Created by wangcheng on 2021/3/23 11:08
 */
public class ComboPaymentService extends ChannelBaseService {
    @Override
    public Object callPreProc(TxnInfo txnInfo, ChannelCallCfg channelCallCfg, OrderInfo orderInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        String transDate = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_DATE);
        String transTime = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_TIME);

        /* 获取营销产品代码 */
        String mompProdKey = "";
        SignAcctInfo signAcctInfo = (SignAcctInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.SIGN_ACCT_INFO);
        if (null == signAcctInfo) {
            rglog.warn("签约信息为空，获取营销产品代码时不拼账户类型!");
            mompProdKey = txnInfo.getSrcTxnAuthCfg().getTxnNum() + txnInfo.getSrcTxnAuthCfg().getBizType();
        } else {
            mompProdKey = txnInfo.getSrcTxnAuthCfg().getTxnNum() + txnInfo.getSrcTxnAuthCfg().getBizType() + signAcctInfo.getSignAcctType();
        }
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(txnInfo.getSrcTxnAuthCfg().getInstId(), "OPMS_MOMP_PROD_CODE", mompProdKey);
        if (null == sysParam) {
            rglog.error("无法根据<{}>获取营销产品代码，拒绝交易!", mompProdKey);
            throw new BizException(OltpRpcdEnum.OLTP_PROCESS_ERROR);
        }

        List<PaymentOrderDetailBean> orderDetailList = (List) GlobalTransUtil.getCacheValue(TransCacheConstant.MOMP_PAYMENT_ORDER_DETAIL_LIST);
        ComboPaymentRequest comboPaymentRequest = new ComboPaymentRequest();
        ConvertUtil.convertOutput(comboPaymentRequest);
        /* Step3：业务请求头 */
        Header header = new Header();
        header.setInstId(instId);
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setChanlId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SRC_SYS_ID));
        header.setBizType(AppParamConstant.DEFAULT_BIZ_TYPE);
        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setTraceNo(sysSeqNum + channelCallCfg.getCallIndex());
        header.setTransDate(transDate);
        header.setTransTime(transTime);
        /* Step4：业务请求体 */
        comboPaymentRequest.setHeader(header);
        UserService userService = new UserServiceImpl();
        comboPaymentRequest.setUserId(userService.getUserSessionValue(UserSessionConstant.MOMP_USER_ID));
        comboPaymentRequest.setBizProdCode(sysParam.getParamValue().trim());
        comboPaymentRequest.setMchntOrderId(orderInfo.getUserOrderInfo().getMchntOrderId());
        comboPaymentRequest.setTransAmt(orderInfo.getUserOrderInfo().getNeedPayAmt());
        if (null == orderDetailList) {
            /* 获取费率类型 */
            String feeType = (null == signAcctInfo) ? "" : convFeeType(signAcctInfo.getSignAcctType());
            comboPaymentRequest.setOrderDetailList(convPaymentOrderDetailInfo(txnInfo, orderInfo.getMchntOrderInfoList(), feeType));
        } else {
            comboPaymentRequest.setOrderDetailList(orderDetailList);
        }
        Object object = GlobalTransUtil.getCacheValue(TransCacheConstant.COUPON_INFO);
        if (null != object) {
            CouponInfo couponInfo = new CouponInfo();
            BeanUtil.beanCopy(object, couponInfo);
            comboPaymentRequest.setCouponInfoList(convPaymentOrderCouponInfo(Collections.singletonList(couponInfo)));
        }
        rglog.debug("comboPaymentRequest:{},", JSON.toJSONString(comboPaymentRequest));
        return comboPaymentRequest;
    }

    @Override
    public PayOrderInfo generatePayOrderInfo(ChannelCallCfg channelCallCfg, OrderInfo orderInfo, Object request) throws Exception {
        ComboPaymentRequest comboPaymentRequest = (ComboPaymentRequest) request;
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        BeanUtil.beanCopy(orderInfo.getUserOrderInfo(), payOrderInfo);
        if (orderInfo.getMchntOrderInfoList().size() == 1) {
            payOrderInfo.setMchntNo(orderInfo.getMchntOrderInfoList().get(0).getMchntNo());
        } else {
            payOrderInfo.setMchntNo(orderInfo.getUserOrderInfo().getGroupMchntNo());
        }
        payOrderInfo.setTxnNum(channelCallCfg.getTxnNum());
        payOrderInfo.setBizType(channelCallCfg.getBizType());
        payOrderInfo.setSysSeqNum(orderInfo.getUserOrderInfo().getSysSeqNum());
        payOrderInfo.setOrderCcy(orderInfo.getUserOrderInfo().getOrderCcy());
        payOrderInfo.setMsgDstId(channelCallCfg.getCallChannelId());
        payOrderInfo.setDstCallIndex(channelCallCfg.getCallIndex());
        payOrderInfo.setChannelSeqNum(comboPaymentRequest.getHeader().getTraceNo());
        payOrderInfo.setChannelReconKey(comboPaymentRequest.getHeader().getTraceNo());
        payOrderInfo.setDstTxnNum(channelCallCfg.getCallChannelTxn());
        payOrderInfo.setPayType(PayTypeEnum.OTHER.getPayType());
        payOrderInfo.setPayerAcctType(AcctTypeEnum.ALL.getAcctType());
        payOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        payOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
        payOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        payOrderInfo.setUserId(orderInfo.getUserOrderInfo().getUserId());
        if (null != comboPaymentRequest.getCouponInfoList()) {
            payOrderInfo.setPayerRemark(JSON.toJSONString(comboPaymentRequest.getCouponInfoList()));
        }
        return payOrderInfo;
    }

    @Override
    public Object callProc(ChannelCallCfg channelCallCfg, Object request) throws Exception {
        ComboPaymentRequest comboPaymentRequest = (ComboPaymentRequest) request;
        /* Step1：调用营销模块联合营销接口，获取对客及对商户手续费营销结果 */
        Result<ComboPaymentResponse> callResult;
        DubboService dubboService = new DubboServiceImpl();
        if (AppParamConstant.YES.equals(channelCallCfg.getSyncCallFlag())) {
            /* Step1.1：异步调用 */
            int iReturnCode;
            int callTimes = 0 == channelCallCfg.getSyncQueryRepeatNum() ? 1 : channelCallCfg.getSyncQueryRepeatNum();
            int gatTime = channelCallCfg.getSyncQueryRepeatTime();
            int gatType = StringUtil.isEmptyOrNull(channelCallCfg.getSyncQueryGapType()) ? 0 : Integer.parseInt(channelCallCfg.getSyncQueryGapType());
            if (ChannelCallModeEnum.LOCAL.getMode().equals(channelCallCfg.getChannelCallMode())) {
                /* Step1.1.1：异步应用内调用 */
                Class clasz = Class.forName(ShareServiceConstant.MOMP_PAYMENT_SERVICE_INNER_PATH);
                iReturnCode = dubboService.asynCallDubboInnerMethod(clasz, ShareServiceConstant.MOMP_COMBO_PAYMENT_METHOD, comboPaymentRequest, channelCallCfg.getSyncResultServiceName(), callTimes, gatType, gatTime);
            } else if (ChannelCallModeEnum.REMOTE.getMode().equals(channelCallCfg.getChannelCallMode())) {
                /* Step1.1.2：异步远程调用 */
                iReturnCode = dubboService.asynCallDubboService(ShareServiceConstant.MOMP_PAYMENT_SERVICE, ShareServiceConstant.MOMP_COMBO_PAYMENT_METHOD, comboPaymentRequest, channelCallCfg.getSyncResultServiceName(), callTimes, gatType, gatTime);
            } else {
                rglog.error("不支持的通道服务调用方式:{}，交易拒绝!", channelCallCfg.getChannelCallMode());
                throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR);
            }
            if (0 == iReturnCode) {
                return new BizResponse<>(OltpRpcdEnum.TRANS_SUCCESS.getRespCode(), OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            } else {
                return new BizResponse<>(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            }
        } else {
            /* Step1.2：同步调用 */
            if (ChannelCallModeEnum.LOCAL.getMode().equals(channelCallCfg.getChannelCallMode())) {
                /* Step1.2.1：同步应用内调用 */
                Class clasz = Class.forName(ShareServiceConstant.MOMP_PAYMENT_SERVICE_INNER_PATH);
                //rglog.error("clasz:{},",clasz.toString());
                callResult = (Result<ComboPaymentResponse>) dubboService.callDubboInnerMethod(clasz, ShareServiceConstant.MOMP_COMBO_PAYMENT_METHOD, comboPaymentRequest);
            } else if (ChannelCallModeEnum.REMOTE.getMode().equals(channelCallCfg.getChannelCallMode())) {
                /* Step1.2.2：同步远程调用 */
                callResult = (Result<ComboPaymentResponse>) dubboService.callDubboService(ShareServiceConstant.MOMP_PAYMENT_SERVICE, ShareServiceConstant.MOMP_COMBO_PAYMENT_METHOD, comboPaymentRequest);
            } else {
                rglog.error("不支持的通道服务调用方式:{}，交易拒绝!", channelCallCfg.getChannelCallMode());
                throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR);
            }
        }
        return callResult;
    }

    @Override
    public Object querySyncCallResult(PayOrderInfo payOrderInfo, Object request) {
        return null;
    }

    @Override
    public OrderInfo callResultOrderProc(OrderInfo orderInfo, String payOrderIndex, TransStatusEnum transStatusEnum, Object response) {
        OrderInfo updateOrderInfo = new OrderInfo();
        Result<ComboPaymentResponse> callResult = (Result<ComboPaymentResponse>) response;
        Optional<PayOrderInfo> payOrderInfoOpt = orderInfo.getPayOrderInfoList().stream().filter(item -> item.getDstCallIndex().equals(payOrderIndex)).findFirst();
        if (payOrderInfoOpt.isPresent() && null != callResult && null != callResult.getResult()) {
            ComboPaymentResponse comboPaymentResponse = callResult.getResult();
            PayOrderInfo payOrderInfo = payOrderInfoOpt.get();
            payOrderInfo.setTransStatus(transStatusEnum.getStatus());
            payOrderInfo.setRespCode(comboPaymentResponse.getRespCode());
            payOrderInfo.setRespDesc(comboPaymentResponse.getRespDesc());
            payOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            switch (transStatusEnum) {
                case TRANS_SUCC:
                    payOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                    /* 用户订单信息处理 */
                    userOrderInfoProcess(orderInfo.getUserOrderInfo(), payOrderInfo, comboPaymentResponse);
                    updateOrderInfo.setUserOrderInfo(orderInfo.getUserOrderInfo());
                    boolean marketingToUserFlag = false;
                    if (AmountUtil.compare(comboPaymentResponse.getTransAmt(), comboPaymentResponse.getTxnAmt()) != 0) {
                        marketingToUserFlag = true;
                    }
                    /* 商户订单信息处理 */
                    if (null == comboPaymentResponse.getOrderDetailList() || comboPaymentResponse.getOrderDetailList().isEmpty()) {
                        rglog.error("调用营销返回结果数据异常，订单明细未返回，按交易超时处理!");
                        payOrderInfo.setTransStatus(TransStatusEnum.TRANS_TIMEOUT.getStatus());
                        payOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
                    } else {
                        String channelDiscountAmt = "0.00";
                        for (MchntOrderInfo mchntOrderInfo : orderInfo.getMchntOrderInfoList()) {
                            Optional<PaymentOrderDetailBean> paymentOrderDetailBeanOpt = comboPaymentResponse.getOrderDetailList().stream().filter(item -> item.getMchntNo().equals(mchntOrderInfo.getMchntNo())).findFirst();
                            if (paymentOrderDetailBeanOpt.isPresent()) {
                                PaymentOrderDetailBean paymentOrderDetailBean = paymentOrderDetailBeanOpt.get();
                                channelDiscountAmt = mchntOrderInfoProcess(mchntOrderInfo, paymentOrderDetailBean, channelDiscountAmt, marketingToUserFlag);
                            } else {
                                rglog.error("调用营销返回结果没有商户号为<{}>的订单明细信息，结果异常，按交易超时处理!");
                                payOrderInfo.setTransStatus(TransStatusEnum.TRANS_TIMEOUT.getStatus());
                                payOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
                            }
                        }
                        payOrderInfo.setChannelDiscountAmt(channelDiscountAmt);
                        payOrderInfo.setSettleDate(DateUtil.getCurrentDate());
                        updateOrderInfo.setMchntOrderInfoList(orderInfo.getMchntOrderInfoList());
                    }
                    break;
                case TRANS_TIMEOUT:
                    payOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
                    break;
                case SYSTEM_REJECT:
                    payOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    /* d0实时入账手续费四舍五入处理 */
                    if (AppParamConstant.YES.equals(orderInfo.getMchntOrderInfoList().get(0).getRealSettleFlag())) {
                        for (MchntOrderInfo mchntOrderInfo : orderInfo.getMchntOrderInfoList()) {
                            mchntOrderInfo.setMchntDiscountFee("0.00");
                            mchntOrderInfo.setRealMchntFee(AmountUtil.rounding(mchntOrderInfo.getRealMchntFee(), 2));
                            mchntOrderInfo.setMchntFee(AmountUtil.rounding(mchntOrderInfo.getMchntFee(), 2));
                            mchntOrderInfo.setMchntSettleAmt(AmountUtil.subtraction(mchntOrderInfo.getRealSettleAmt(), mchntOrderInfo.getRealMchntFee()));
                        }
                        updateOrderInfo.setMchntOrderInfoList(orderInfo.getMchntOrderInfoList());
                    }
                    break;
                default:
                    break;
            }
            updateOrderInfo.setPayOrderInfoList(Collections.singletonList(payOrderInfo));
            return updateOrderInfo;
        }
        return null;
    }

    /**
     * 用户订单信息赋值处理
     *
     * @param userOrderInfo        用户订单信息
     * @param payOrderInfo         支付订单信息
     * @param comboPaymentResponse 营销通道调用结果
     */
    private void userOrderInfoProcess(UserOrderInfo userOrderInfo, PayOrderInfo payOrderInfo, ComboPaymentResponse comboPaymentResponse) {
        /* 用户实际支付金额及对客优惠金额 */
        userOrderInfo.setRealPayAmt(comboPaymentResponse.getTxnAmt());
        userOrderInfo.setRealDiscountAmt(AmountUtil.subtraction(userOrderInfo.getOrderAmt(), comboPaymentResponse.getTxnAmt()));
        /* 用户核销的卡券信息 */
        if (null != payOrderInfo.getPayerRemark()) {
            List<CouponInfo> couponInfoList = JSON.parseArray(payOrderInfo.getPayerRemark(), CouponInfo.class);
            Map<String, String> couponInfoMap = new HashMap<>();
            for (CouponInfo couponInfo : couponInfoList) {
                couponInfoMap.put(couponInfo.getDiscountId(), couponInfo.getDiscountAmt());
            }
            userOrderInfo.setDiscountInfo(JSON.toJSONString(couponInfoMap));
        }
    }

    /**
     * 商户订单信息赋值处理
     *
     * @param mchntOrderInfo         商户订单信息
     * @param paymentOrderDetailBean 营销订单明细信息
     * @param channelDiscountAmt     通道优惠金额
     * @param marketingToUserFlag    对客营销标志：true-有对客营销，false-没有对客营销
     * @return 通道优惠金额
     */
    private String mchntOrderInfoProcess(MchntOrderInfo mchntOrderInfo, PaymentOrderDetailBean paymentOrderDetailBean, String channelDiscountAmt, boolean marketingToUserFlag) {
        String mchntNoRechargeMarketingAmt = "0.00";
        /* 满减活动商户免充值对客营销金额 */
        if ("0".equals(paymentOrderDetailBean.getMoneyOffSettlementMethod())) {
            mchntNoRechargeMarketingAmt = AmountUtil.addition(mchntNoRechargeMarketingAmt, paymentOrderDetailBean.getMoneyOffSubsidyMchnt());
        } else {
            channelDiscountAmt = AmountUtil.addition(channelDiscountAmt, paymentOrderDetailBean.getMoneyOffSubsidyTotal());
        }
        /* 卡卷活动商户免充值对客营销金额 */
        if ("0".equals(paymentOrderDetailBean.getCouponMchntSettleMethod())) {
            mchntNoRechargeMarketingAmt = AmountUtil.addition(mchntNoRechargeMarketingAmt, paymentOrderDetailBean.getCouponMchntSubsidy());
        } else {
            channelDiscountAmt = AmountUtil.addition(channelDiscountAmt, paymentOrderDetailBean.getCouponTotalSubsidy());
        }
        /* 商户免充值对客营销金额、商户实际清算本金以及商户核销卡卷信息赋值 */
        mchntOrderInfo.setCustomerMarketAmt(mchntNoRechargeMarketingAmt);
        mchntOrderInfo.setRealSettleAmt(AmountUtil.subtraction(mchntOrderInfo.getRealSettleAmt(), mchntNoRechargeMarketingAmt));
        mchntOrderInfo.setRedeemCouponIds(paymentOrderDetailBean.getCouponNo());
        /* 商户实收手续费、商户优惠手续费及商户清算净额赋值 */
        if (!StringUtil.isEmptyOrNull(paymentOrderDetailBean.getMchntFeeActual()) && AmountUtil.compare(paymentOrderDetailBean.getMchntFeeActual(), "0.00") == 1) {
            mchntOrderInfo.setRealMchntFee(AmountUtil.rounding(paymentOrderDetailBean.getMchntFeeActual(), 2));
        } else {
            mchntOrderInfo.setRealMchntFee(AmountUtil.rounding(mchntOrderInfo.getMchntFee(), 2));
        }
        mchntOrderInfo.setMchntDiscountFee(AmountUtil.subtraction(mchntOrderInfo.getMchntFee(), mchntOrderInfo.getRealMchntFee()));
        mchntOrderInfo.setMchntSettleAmt(AmountUtil.subtraction(mchntOrderInfo.getRealSettleAmt(), mchntOrderInfo.getRealMchntFee()));
        /* 只要有对客或对商户手续费营销，商户就不享受D0实时清算 */
        if (marketingToUserFlag || AmountUtil.compare(mchntOrderInfo.getMchntDiscountFee(), "0.00") != 0) {
            mchntOrderInfo.setRealSettleFlag(AppParamConstant.NO);
        }
        mchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        return channelDiscountAmt;
    }

    /**
     * 转换营销模块订单明细信息
     *
     * @param txnInfo            交易配置信息
     * @param mchntOrderInfoList 商户订单信息
     * @param feeType            费率类型
     * @return 营销模块订单明细信息
     */
    private List<PaymentOrderDetailBean> convPaymentOrderDetailInfo(TxnInfo txnInfo, List<MchntOrderInfo> mchntOrderInfoList, String feeType) {
        List<PaymentOrderDetailBean> paymentOrderDetailBeanList = new ArrayList<>();
        for (MchntOrderInfo mchntOrderInfo : mchntOrderInfoList) {
            PaymentOrderDetailBean paymentOrderDetailBean = new PaymentOrderDetailBean();
            paymentOrderDetailBean.setMchntNo(mchntOrderInfo.getMchntNo());
            if (mchntOrderInfo.getOrderDiscountAmt() != null && !mchntOrderInfo.getOrderDiscountAmt().isEmpty()) {
                paymentOrderDetailBean.setOrderAmt(AmountUtil.subtraction(mchntOrderInfo.getOrderAmt(), mchntOrderInfo.getOrderDiscountAmt()));
            } else {
                paymentOrderDetailBean.setOrderAmt(mchntOrderInfo.getOrderAmt());
            }
            paymentOrderDetailBean.setFeeInputMode(mchntOrderInfo.getFeeInputMode());
            paymentOrderDetailBean.setMchntFeeReceivableRate(mchntOrderInfo.getFeeRate());
            paymentOrderDetailBean.setMchntSettleAmt(mchntOrderInfo.getRealSettleAmt());
            paymentOrderDetailBean.setMchntFeeReceivable(mchntOrderInfo.getMchntFee());
            paymentOrderDetailBean.setFeeType(feeType);
            paymentOrderDetailBean.setBizProdCode(txnInfo.getSrcTxnAuthCfg().getBizProdCode());
            paymentOrderDetailBeanList.add(paymentOrderDetailBean);
        }
        return paymentOrderDetailBeanList;
    }

    /**
     * 转换营销卡券信息
     *
     * @param couponInfoList 业务请求卡券信息
     * @return 营销模块卡卷信息
     */
    private List<PaymentOrderCouponInfoBean> convPaymentOrderCouponInfo(List<CouponInfo> couponInfoList) {
        if (null != couponInfoList && !couponInfoList.isEmpty()) {
            List<PaymentOrderCouponInfoBean> paymentOrderCouponInfoBeanList = new ArrayList<>();
            for (CouponInfo couponInfo : couponInfoList) {
                PaymentOrderCouponInfoBean paymentOrderCouponInfoBean = new PaymentOrderCouponInfoBean();
                paymentOrderCouponInfoBean.setCouponNo(couponInfo.getDiscountId());
                paymentOrderCouponInfoBeanList.add(paymentOrderCouponInfoBean);
            }
            return paymentOrderCouponInfoBeanList;
        } else {
            return null;
        }
    }

    /**
     * 根据支付账户类型转换费率类型
     *
     * @param payerAcctType 支付账户类型
     * @return 费率类型
     */
    private String convFeeType(String payerAcctType) {
        String feeType;
        if (AcctTypeEnum.BANK_CREDIT.getAcctType().equals(payerAcctType)) {
            feeType = FeeTypeEnum.BANK_CR_CARD.getFeeType();
        } else if (AcctTypeEnum.BANK_DEBIT.getAcctType().equals(payerAcctType)) {
            feeType = FeeTypeEnum.BANK_DR_CARD.getFeeType();
        } else if (AcctTypeEnum.OTHER_DEBIT.getAcctType().equals(payerAcctType)) {
            feeType = FeeTypeEnum.OTHER_BANK_DR_CARD.getFeeType();
        } else {
            feeType = FeeTypeEnum.OTHER_BANK_CR_CARD.getFeeType();
        }
        return feeType;
    }
}
